package kafeisc.web.servlet.client;

import kafeisc.domain.User;
import kafeisc.utils.DataSourceUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * @Classname ${NAME}
 * @Description TODO
 * @Date 2020/5/19 21:54
 * @Created 
 */
@WebServlet(name = "ChangeInfoServlet")
public class ChangeInfoServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        request.setCharacterEncoding("utf-8");
        String passwordSelect = request.getParameter("password2");
        String sex = request.getParameter("radiobutton");
        String phone = request.getParameter("phone");
        User user = (User) request.getSession().getAttribute("user");
        String sql = "update user set password=?,gender=?,telephone=? where username='" + user.getUsername() + "'";
        Connection conn = null;
        try {
            conn = DataSourceUtils.getConnection();
            PreparedStatement pstm1 = conn.prepareStatement(sql);
            pstm1.setString(1,passwordSelect);
            pstm1.setString(2,sex);
            pstm1.setString(3,phone);
            int i = pstm1.executeUpdate();
            if(i > 0){
                // 修改成功
                pstm1.close();
                conn.close();
                response.sendRedirect(request.getContextPath() + "/client/changeInfoSuccess.jsp");
            }
            else{
                // 修改失败
                pstm1.close();
                conn.close();
                response.sendRedirect(request.getContextPath() + "/client/changeInfoFail.jsp");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }
}
